<template>
  <!-- 公海池页 -->
  <div>
    <!-- 高级搜索框架 -->
    <!-- 高级搜索头部 -->
    <div class="header">
      <span>高级搜索</span>
    </div>

    <!-- 搜索框 -->
    <div class="headerInfo">
      <el-form ref="form" label-width="80px">
        <el-row>
          <el-col
            :span="8"
          ><div class="grid-content bg-purple" />
            <el-form-item label="客户姓名:">
              <el-input v-model="form.name" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col
            :span="8"
          ><div class="grid-content bg-purple-light" />
            <el-form-item label="手机号:">
              <el-input v-model="form.phone" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col
            :span="8"
          ><div class="grid-content bg-purple" />
            <el-form-item label="客户ID:">
              <el-input v-model="form.id" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row class="rows">
          <el-col
            :span="8"
          ><div class="grid-content bg-purple" />
            <el-form-item label="意向学科:">
              <el-select v-model="form.subject" placeholder="请选择">
                <el-option label="Java" value="0" />
                <el-option label="前端" value="1" />
                <el-option label="人工智能" value="2" />
                <el-option label="大数据" value="3" />
                <el-option label="Python" value="4" />
                <el-option label="测试" value="6" />
                <el-option label="新媒体" value="7" />
                <el-option label="产品经理" value="8" />
                <el-option label="UI设计" value="9" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col
            :span="8"
          ><div class="grid-content bg-purple-light" />
            <el-form-item label="创建时间:">
              <el-col :span="11">
                <el-date-picker
                  v-model="form.createTime"
                  style="width: 285px"
                  type="datetime"
                  placeholder="选择日期"
                />
              </el-col>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <!-- 按钮 -->
      <div class="elbutton">
        <el-button class="cancellation" @click="resetting">重置</el-button>
        <el-button type="primary" class="create" @click="search">搜索</el-button>
      </div>
    </div>

    <!-- 新建商机/批量分配 -->
    <div class="newbutton">
      <el-button type="primary" :disabled="num<=1?true:false" @click="fishout">批量捞取</el-button>
      <el-button type="primary" @click="allocation">批量分配</el-button>
    </div>

    <!-- 表格数据 -->
    <el-table
      v-loading="isloading"
      :data="list"
      tooltip-effect="dark"
      style="width: 100%; margin-top: 10px;"
      :cell-style="{ 'text-align': 'center' }"
      :header-cell-style="{ 'text-align': 'center' }"
      @select="onRowclick"
      @select-all="onRowclick"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="id" label="客户ID" width="120" />
      <el-table-column prop="name" label="姓名" width="120" />
      <el-table-column
        prop="phone"
        label="手机号"
        show-overflow-tooltip
      />
      <el-table-column
        prop="subject"
        label="意向学科"
        show-overflow-tooltip
      >
        <template #default="{row}">
          <span v-if="row.subject === '0'">Java</span>
          <span v-if="row.subject === '1'">前端</span>
          <span v-if="row.subject === '2'">人工智能</span>
          <span v-if="row.subject === '3'">大数据</span>
          <span v-if="row.subject === '4'">Python</span>
          <span v-if="row.subject === '6'">测试</span>
          <span v-if="row.subject === '7'">新媒体</span>
          <span v-if="row.subject === '8'">产品经理</span>
          <span v-if="row.subject === '9'">UI设计</span>
          <span v-if="row.subject === null">- - -</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        show-overflow-tooltip
        sortable
      >
        <template #default="{row}">
          {{ createTimes(row.createTime) }}
        </template>
      </el-table-column>

      <el-table-column label="操作" show-overflow-tooltip>
        <el-button class="buttons2" :disabled="num!=1?true:false" @click="fishout">捞取</el-button>
        <el-button class="buttons2" style="margin-left:-20px" @click="allocation">分配</el-button>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="paging">
      <br class="Apple-interchange-newline">
      <div class="page-container">
        <span class="Selection">已选择{{ num }}项</span>
        <el-pagination
          background
          :current-page="Number(form.page)"
          :page-sizes="[10, 50, 100, 150]"
          :page-size="Number(form.pageSize)"
          layout="total, prev, pager, next, sizes,jumper"
          :total="Number(total)"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 分配商机 -->
    <addAssignClues :clues-dialog.sync="cluesDialog" :title="title" />
  </div>
</template>

<script>
import { getcomMercesecond, fishoutcomMercesecond } from '@/api/comMercesecond'
import dayjs from 'dayjs'
import addAssignClues from './addAssignClues.vue'
export default {
  components: {
    addAssignClues
  },
  data() {
    return {
      // tab栏默认选中
      activeName: 'first',
      shanghai: '',
      // 获取接口数据
      list: [],
      form: {
        id: '',
        name: '',
        phone: '',
        subject: '',
        createTime: '',
        pageNum: 1,
        pageSize: 10
      },
      total: null,
      // 分配商机弹窗
      cluesDialog: false,
      // 用于判断分配商机或分配线索
      title: '',
      // 选中多选框的数量
      num: 0,
      // 加载
      isloading: false,
      // 用于存捞取的数据
      fishouts: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /**
     * 捞取
     */
    fishout() {
      this.$confirm(`您已选择${this.num}条数据，确认捞取?`, '提示').then(async() => {
        // console.log(this.fishouts.length)
        // console.log(this.fishouts[0].id)
        // 存id 用于传参
        const ids = []
        // 判断是单个捞取 还是批量捞取
        if (this.fishouts.length === 1) {
          // 单个捞取
          const id = this.fishouts[0].id
          // 存到ids数组里
          ids.push(id)
        } else {
          // 多个捞取 forEach拿多个需要捞取的id
          this.fishouts.forEach((e, i) => {
            // 拿到没一项的id
            const id = this.fishouts[i].id
            // 把每一项添加到ids数组里
            ids.push(id)
          })
        }
        // 调用捞取接口
        await fishoutcomMercesecond(ids)
        // 成功提醒
        this.$message.success('捞取成功')
        // 清除选中数量
        this.num = 0
        // 重新渲染
        this.getList()
      }).catch(() => {})
    },
    /**
     * 拿多选框的数据条数
     */
    onRowclick(selection, row) {
      this.num = selection.length
      console.log(selection, row)
      this.fishouts = selection
    },
    /**
     * 批量分配商机
     */
    allocation() {
      this.title = '分配商机'
      this.cluesDialog = true
    },
    /**
     * 重置
     */
    resetting() {
      this.form = {
        id: '',
        name: '',
        phone: '',
        subject: '',
        createTime: '',
        pageNum: 1,
        pageSize: 10
      }
      this.getList()
    },
    /**
     * 搜索
     */
    search() {
      this.getList()
    },
    /**
     * 处理创建时间
     */
    createTimes(time) {
      return time ? dayjs(time).format('YYYY-MM-DD HH:MM:ss') : ''
    },
    /**
     * 获取数据
     */
    async getList() {
      this.isloading = true
      const src = await getcomMercesecond(this.form)
      // console.log(src)
      this.list = src.data.records
      this.total = src.data.total
      this.isloading = false
    },
    /**
     * 切换页
     */
    handleCurrentChange(val) {
      this.form.pageNum = val
      this.getList()
    },
    /**
     * 切换页容量
     */
    handleSizeChange(val) {
      this.form.pageSize = val
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.header {
  width: 100%;
  height: 50px;
  margin-top: 10px;
  border-radius: 5px;
  line-height: 50px;
  background-color: #f2f2f2;
  span {
    margin-left: 30px;
  }
}
.headerInfo {
  width: 100%;
  height: 200px;
  margin-top: -5px;
  padding-top: 10px;
  border: 2px solid #f2f2f2;
}
::v-deep .el-row {
  margin: 0 20px;
  margin-top: 10px;
}
.rows {
  margin: 0 20px;
  margin-top: 0px;
}
::v-deep .el-input__inner {
  width: 285px;
}
::v-deep .el-form-item__label {
  color: #000;
  font-weight: 400;
}
.elbutton {
  float: right;
  margin-right: 25px;
  margin-top: -10px;
  .cancellation {
    width: 100px;
    margin-top: 5px;
  }
  .create {
    width: 100px;
    margin-top: 5px;
  }
}
.newbutton {
  margin-top: 20px;
  button {
    width: 120px;
  }
}
::v-deep .cell {
  font-weight: 400;
  color: #000;
}
.buttons {
  color: #3366ff;
  border: none;
  background: transparent;
}
::v-deep .el-input__inner {
  width: 250px;
}
::v-deep .el-pagination__sizes input {
  width: 90px;
}
::v-deep .el-pagination__jump input {
  width: 20px !important;
}
.paging {
  padding-right: 50px;
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
.Selection {
  float: left;
  line-height: 40px;
  font-size: 14px;
}
.buttons2 {
  color: #02860f;
  border: none;
  background: transparent;
}
</style>
